package com.defect.controller.statistics;

import com.defect.bo.ResultBo;
import com.defect.service.statisticsService.SWorkLogService;
import com.defect.util.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("worklog")
public class SWorkLogController {
    @Resource
    private SWorkLogService workLogService;

    /**
     * 工时统计日报
     * @param id 项目id
     * @param page 属性pageIndex当前页，属性number每页显示多少数量，属性dataCount数据总数，pageCount当前页的总数
     * @param date1 开始时间
     * @param date2 最后时间
     * @return map集合，属性<"pname",项目名称><"name",登记人><"date",登记时间><"hour",登记工时>
     * @throws Exception
     */
    @RequestMapping("sameday")
    public ResultBo selectWorkLogSameDay(Integer id, Page page, Date date1, Date date2) throws Exception {
        Calendar calendar = Calendar.getInstance();
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        Date beginOfDate = calendar.getTime();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        HashMap<String, Object> map = resultMap(id,page,date1,date2);
        map.put("date", formatter.format(beginOfDate));
        long dataCount = workLogService.count(map);
        page.setDataCount(dataCount);
        return new ResultBo("200", "success", page, workLogService.selectWorkLogSameDay(map));
    }

    /**
     * 工时统计月报
     * @param id 项目id
     * @param page 属性pageIndex当前页，属性number每页显示多少数量，属性dataCount数据总数，pageCount当前页的总数
     * @param date1 开始时间
     * @param date2 最后时间
     * @return map集合，属性<"pname",项目名称><"name",登记人><"date",登记时间><"hour",登记工时>
     * @throws Exception
     */
    @RequestMapping("samemonth")
    public ResultBo selectWorkLogSameMonth(Integer id, Page page, Date date1, Date date2) throws Exception {
        Calendar calendar2 = Calendar.getInstance();
        calendar2.set(calendar2.get(Calendar.YEAR), calendar2.get(Calendar.MONTH), 1, 0, 0, 0);
        Date beginOfDate2 = calendar2.getTime();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        HashMap<String, Object> map = resultMap(id,page,date1,date2);
        map.put("date", formatter.format(beginOfDate2));
        long dataCount = workLogService.count(map);
        page.setDataCount(dataCount);
        List<Map<String, Object>> list = workLogService.selectWorkLogSameDay(map);
        String name = "";
        List<Map<String, Object>> arr = new ArrayList<Map<String, Object>>();
        int num = 0;
        for (int i = 0; i <list.size() ; i++) {
            if(name.equals((String)list.get(i).get("pname"))){
                arr.add(list.get(i));
                list.remove(i);
                i--;
            }else{
                num = i ;
                list.get(i).put("children", null);
                name=(String)list.get(i).get("pname");
                if(num>0){
                    list.get(num-1).put("children",arr);
                    arr = new ArrayList<Map<String, Object>>();
                }
            }
            if((i+1)==list.size()){
                list.get(num).put("children",arr);
            }
        }
        return new ResultBo("200", "success", page, list);
    }

    public HashMap<String, Object> resultMap(Integer id, Page page, Date date1, Date date2){
        HashMap<String, Object> map = new HashMap<>();
        map.put("id", id);
        map.put("pageIndex", page.getPageIndex());
        map.put("number", page.getNumber());
        map.put("start",date1);
        map.put("end",date2);
        return map;
    }

}
